<?php
/*!
 * LostKids PHP Library v0.1 
 * http://huali.li/
 *
 * Copyright 2011, 
 * Written By James Pang
 *  
 * Date: 2011/2/14
 */

if (!session_id()) {
    session_start();
}

class DataManagerImpl implements DataManager
{
    public function __construct() {
    }

    /**
     * @return 0 if non
     */
    public function getLastMsgCursor($dataSrcKey) {
    	return 1;@$_SESSION[$dataSrcKey];
    }

    public function saveLastMsgCursor($dataSrcKey, $cursor) {
        $_SESSION[$dataSrcKey] = $cursor;
    }

    public function getMsg($lid){
        return $_SESSION['msgs'][$lid];
    }

    public function saveMsg($msg){
        if(isset($_SESSION['msgs']))
            return;
    	@$_SESSION['msgs'][] = $msg;
        $msg->id = count($_SESSION['msgs']) - 1;
    }

    public function saveUsersMatch($msgLids, $tobeMatched){
        foreach($msgLids as $lid) {
            @$_SESSION['msgs'][$lid]->setMatchId($tobeMatched);
        }
    }

    public function createMatchPanel($panelKey, $tobeMatched){
    	@$_SESSION['matchPanel'][$panelKey] = $tobeMatched;
    }

    /**
     * @see BatchMessage
     */
    public function getBatchMsgs($msg){
        $ret = array();

        $key = BatchMessage::genNormBatchKey($msg);
        foreach(@$_SESSION['msgs'] as $m) {
            if($key == BatchMessage::genNormBatchKey($m)){
            	$ret[] = $m;
            }
        }
        
        return $ret;
    }

    public function isUserInFaceDB($userId) {
    }

    /**
     * if msg is batch, update all. else update 1 msg.
     */
    public function updateMsgsStatus($msg, $newMsgStatus) {
        if(empty($msg->ids))
    	    $msg->status = $newMsgStatus;
        else
            foreach($msg->ids as $lid)
                $this->getMsg($lid)->status = $newMsgStatus;
    }
}
?>
